package app.poller.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import app.poller.entities.Option;
import app.poller.entities.Poll;


public class DBHelper extends SQLiteOpenHelper{

	final static int DB_VERSION = 6;
	final static String DB_NAME = "testdb1";
	final static String TABLE_CONTACTS = "contacts";
	final static String TABLE_POLLS = "polls";
	final static String TABLE_OPTIONS = "options";

	final static String COLUMN_ID = "_id";
	final static String COLUMN_POLL = "poll";
	final static String COLUMN_POLL_INVITEE_COUNT = "poll_count";
	final static String COLUMN_POLL_OPTION_COUNT = "poll_option_count";
	final static String COLUMN_POLL_START_TIME = "poll_start_time";
	final static String COLUMN_POLL_DURATION = "poll_duration";

	final static String COLUMN_OPTION_TEXT = "option_text";
	final static String COLUMN_OPTION_SHORTCUT = "option_shortcut";
	final static String COLUMN_OPTION_VOTES = "option_votes";
	final static String COLUMN_OPTION_POLL_ID = "option_poll_id";

			Context mContext;

	public DBHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		mContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_POLLS_TABLE = "CREATE TABLE " + TABLE_POLLS + "("
				+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
				+ COLUMN_POLL + " TEXT,"
				+ COLUMN_POLL_OPTION_COUNT + " INTEGER NOT NULL,"
				+ COLUMN_POLL_INVITEE_COUNT + " INTEGER NOT NULL DEFAULT '0'," 
				+ COLUMN_POLL_START_TIME + " INTEGER NOT NULL, "
				+ COLUMN_POLL_DURATION + " INTEGER NOT NULL"
				+ ")";
		db.execSQL(CREATE_POLLS_TABLE);

		String CREATE_OPTIONS_TABLE = "CREATE TABLE " + TABLE_OPTIONS + "("
				+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
				+ COLUMN_OPTION_TEXT + " TEXT NOT NULL," 
				+ COLUMN_OPTION_SHORTCUT + " TEXT NOT NULL,"
				+ COLUMN_OPTION_VOTES + " INTEGER DEFAULT '0',"
				+ COLUMN_OPTION_POLL_ID + " INTEGER NOT NULL"
				+")";
		db.execSQL(CREATE_OPTIONS_TABLE);
		
		Log.e("tables", "tables created");
	}


	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_POLLS);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_OPTIONS);
		Log.e("tables", "tables dropped");
		
		onCreate(db);
	}


	public void addPoll(Poll poll) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues pollTableValues = new ContentValues();
		pollTableValues.put(COLUMN_POLL, poll.getQuestion());
		pollTableValues.put(COLUMN_POLL_OPTION_COUNT, poll.getOptionList().size());
		pollTableValues.put(COLUMN_POLL_INVITEE_COUNT, poll.getContactList().size());
		pollTableValues.put(COLUMN_POLL_START_TIME, poll.getPollCreatedTime());
		pollTableValues.put(COLUMN_POLL_DURATION, poll.getPollDuration());

		long pollId = db.insert(TABLE_POLLS, null, pollTableValues);
		
		Log.e("tables", "poll added to table");

		for(int i = 0; i < poll.getOptionList().size(); i++) {
			Option option = poll.getOptionList().get(i);
			ContentValues optionTableValues = new ContentValues();
			optionTableValues.put(COLUMN_OPTION_TEXT, option.getOptionName());
			optionTableValues.put(COLUMN_OPTION_SHORTCUT, option.getOptionShortcut());
			optionTableValues.put(COLUMN_OPTION_VOTES, 0);
			optionTableValues.put(COLUMN_OPTION_POLL_ID, pollId);
			
			long optionId = db.insert(TABLE_OPTIONS, null, optionTableValues);
			
			Log.e("option", optionId + "");
		}
		db.close();
	}
}
